<?php

/**
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     @license http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * @package SensAPIlity
 */

/**
 * @author api.jmatt@gmail.com
 * @version 0.1
 */

$currentDir = dirname(__FILE__);
$authSettings = parse_ini_file($currentDir . "/settings.ini");

global $SENSAPILITY_CLIENTS;
global $SENSAPILITY_AUTH;

$developerPassword = null;
$developerEmail = null;
$clientIds = array("ContentAds" => null, 
                   "SearchAds" => null, 
                   "ReferralAds" => null);

if ( !array_key_exists("version", $authSettings) ||
     strcmp(trim($authSettings["version"]), "") == 0 ) {
  echo "SensAPIlity PHP library => Warning: version not " .
    "set in file 'settings.ini', will default to 3.\n";
  $version = 3;
} else {
  $version = $authSettings['version'];
}

if ( !array_key_exists("adSenseAddress", $authSettings) || 
     strcmp(trim($authSettings["adSenseAddress"]), "") == 0 ) {
  echo "SensAPIility PHP library => Warning: Service address not " .
    "set in file 'settings.ini'.\n";
} else {
  $serverAddress = $authSettings['adSenseAddress'] . 'v' .
    $version . '/';
}

if ( !array_key_exists("developerPassword", $authSettings) || 
     strcmp(trim($authSettings["developerPassword"]), "") == 0 ) {
  echo "SensAPIility PHP library => Warning: Developer password not " .
    "set in file 'settings.ini'\n";
} else {
  $developerPassword = $authSettings['developerPassword'];
}

if ( !array_key_exists("developerEmail", $authSettings) || 
     strcmp(trim($authSettings["developerEmail"]), "") == 0 ) {
  echo "SensAPIility PHP library => Warning: Developer email not " . 
    "set in file 'settings.ini'\n";
} else {
  $developerEmail = $authSettings['developerEmail'];
}

if ( array_key_exists("content-client-id", $authSettings) && 
     strcmp(trim($authSettings["content-client-id"]), "") != 0 ) {
  $clientIds["ContentAds"] = $authSettings['content-client-id'];
}

if ( array_key_exists("search-client-id", $authSettings) &&
     strcmp(trim($authSettings["search-client-id"]), "") != 0 ) {
  $clientIds["SearchAds"] = $authSettings['search-client-id'];
}

if ( array_key_exists("referral-client-id", $authSettings) &&
     strcmp(trim($authSettings["referral-client-id"]), "") != 0 ) {
  $clientIds["ReferralAds"] = $authSettings['referral-client-id'];
}

if ( $version == 3 ) {
  require_once($currentDir . "/lib/v3/sensapilityauthentication.php");
  require_once($currentDir . "/sensapilityclients.php");
} elseif ( $version == 2 ) {
  require_once($currentDir . "/lib/sensapilityauthentication.php");
  require_once($currentDir . "/sensapilityclients.php");
}

$SENSAPILITY_CLIENTS = new SensapilityClients($serverAddress);
$SENSAPILITY_AUTH = new SensapilityAuthentication($developerEmail, 
                                                  $developerPassword, 
                                                  $clientIds);
?>
